package com.yang.studentsapp.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.yang.studentsapp.R;
import com.yang.studentsapp.ui.campus.CampusFragment;
import com.yang.studentsapp.ui.friends.FriendsFragemtn;
import com.yang.studentsapp.ui.life.LifeFragment;
import com.yang.studentsapp.ui.me.MineFragment;
import com.yang.studentsapp.ui.message.FastmsgFragment;
import com.yang.studentsapp.utils.RQManager;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by ${Ян-Вэнь-Чао} on 2016/9/30.
 */
public class MainActivity extends AppCompatActivity {
    FragmentManager fm = null;
    CampusFragment mCampusFragment;
    LifeFragment mLifeFragment;
    FriendsFragemtn mFriendsFragemtn;
    FastmsgFragment mFastmsgFragment;
    MineFragment mMineFragment;

    RadioGroup radioGroup;

    RadioButton radioCampus;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        RQManager.init(this);
        ButterKnife.bind(this);
        fm = getSupportFragmentManager();
        initView();
    }

    private void initView() {
        radioGroup = (RadioGroup) findViewById(R.id.radio_group);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                FragmentTransaction ft = fm.beginTransaction();
                hide(ft);
                switch (checkedId) {
                    case R.id.radio_campus:
                        if (mCampusFragment == null) {
                            mCampusFragment = new CampusFragment();
                            ft.add(R.id.fl_main, mCampusFragment);
                        }
                        ft.show(mCampusFragment);
                        break;
                     case R.id.radio_life:
                         if (mLifeFragment == null) {
                             mLifeFragment = new LifeFragment();
                             ft.add(R.id.fl_main, mLifeFragment);
                         }
                         ft.show(mLifeFragment);
                        break;
                     case R.id.radio_friend:
                         if (mFriendsFragemtn == null) {
                             mFriendsFragemtn = new FriendsFragemtn();
                         ft.add(R.id.fl_main, mFriendsFragemtn);
                     }
                         ft.show(mFriendsFragemtn);

                        break;
                     case R.id.radio_fastmsg:
                         if (mFastmsgFragment == null) {
                             mFastmsgFragment = new FastmsgFragment();
                             ft.add(R.id.fl_main, mFastmsgFragment);
                         }
                         ft.show(mFastmsgFragment);
                        break;
                     case R.id.radio_mine:
                         if (mMineFragment == null) {
                             mMineFragment = new MineFragment();
                             ft.add(R.id.fl_main, mMineFragment);
                         }
                         ft.show(mMineFragment);
                        break;

                }
                ft.commit();
            }
        });
        radioCampus = (RadioButton) findViewById(R.id.radio_campus);
        radioCampus.setChecked(true);
    }

    private void hide(FragmentTransaction ft) {
        if (mCampusFragment != null) {
            ft.hide(mCampusFragment);
        }
         if (mLifeFragment != null) {
            ft.hide(mLifeFragment);
        }
         if (mFriendsFragemtn != null) {
            ft.hide(mFriendsFragemtn);
        }
         if (mFastmsgFragment != null) {
            ft.hide(mFastmsgFragment);
        }
         if (mMineFragment != null) {
            ft.hide(mMineFragment);
        }

    }

}
